package com.qiantu.phone.ui.activity.map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

public class MapOverlyView extends View {
    private Paint circlePaint, bitmapPiant;
    private int width, height;
    private int radius = 50;
    private Bitmap centerBitmip;
    private int bitmapWidth = 0;
    public MapOverlyView(Context context) {
        super(context);
        initView(context, null);

    }

    public MapOverlyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);

    }

    private void initView(Context context, AttributeSet attrs){
        circlePaint = new Paint();
        circlePaint.setDither(true);
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(AppApplication.getInstance().getThemeColor(R.attr.alpha1AThemeColor));

        bitmapPiant = new Paint();
        bitmapPiant.setAntiAlias(true);
        bitmapPiant.setDither(true);
        centerBitmip = ((BitmapDrawable)AppApplication.getInstance().getThemeDrawable(R.attr.icon_map_positioning)).getBitmap();
        bitmapWidth = (int)getResources().getDimension(R.dimen.dp_32);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
        int centerX = width / 2;
        int centerY = height / 2;
        canvas.drawCircle(centerX, centerY, radius, circlePaint);
        Rect rect = new Rect(centerX - centerBitmip.getWidth()/2, centerY - centerBitmip.getHeight()/2, centerX + centerBitmip.getWidth()/2, centerY + centerBitmip.getHeight()/2);
        canvas.drawBitmap(centerBitmip, null, rect, bitmapPiant);
    }

    public void setRadius(int radius){
        this.radius = radius;
        invalidate();
    }
}
